<?php

/**
 * This is the model class for table "Users".
 *
 * The followings are the available columns in table 'Users':
 * @property string $id
 * @property integer $type
 * @property string $first_name
 * @property string $second_name
 * @property string $email
 * @property string $password
 * @property integer $is_deleted
 *
 * The followings are the available model relations:
 * @property KeeperReservations[] $keeperReservations
 * @property StudentReservations[] $studentReservations
 * @property TeacherReservations[] $teacherReservations
 */
class Users extends CActiveRecord
{
	public $repeat_password;
	
	protected function beforeSave() {
		if($this->password != '') {
			$this->password = $this->encrypt($this->password);
		} else {
			unset($this->fld_password);
		}
		return parent::beforeSave();
	}
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Users the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'Users';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('first_name, second_name, email, type', 'required'),
			array('email', 'unique', 'attributeName'=> 'email', 'caseSensitive' => 'false', 'on' => 'insert'),
			array('password', 'required', 'on' => 'create'),
			array('type, is_deleted', 'numerical', 'integerOnly'=>true),
			array('first_name', 'length', 'max'=>20),
			array('second_name', 'length', 'max'=>30),
			array('email', 'length', 'max'=>70),
			array('email', 'email'),
			array('password', 'length', 'max'=>128),
			array('password', 'compare', 'compareAttribute' => 'repeat_password', 'on' => 'update'),
			array('repeat_password', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('type, first_name, second_name, email, is_deleted', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'keeperReservations' => array(self::HAS_MANY, 'KeeperReservations', 'user_id'),
			'studentReservations' => array(self::HAS_MANY, 'StudentReservations', 'user_id'),
			'teacherReservations' => array(self::HAS_MANY, 'TeacherReservations', 'user_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'type' => 'Typ',
			'first_name' => 'Imię',
			'second_name' => 'Nazwisko',
			'email' => 'Email',
			'password' => 'Hasło',
			'repeat_password' => 'Powtórz hasło'
		);
	}
	public function search() {
		$criteria=new CDbCriteria;
		$criteria->compare('type',$this->type);
		$criteria->compare('first_name',$this->first_name,true);
		$criteria->compare('second_name',$this->second_name,true);
		$criteria->compare('email',$this->email,true);
		$criteria->compare('is_deleted', 0);
		$criteria->addCondition('type > 1');
		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	
	public function encrypt($password)
	{
		return md5($password);
	}

	public function generate_password($len) {
		$pattern_letters = 'qwertyupasdfghjkzxcvbnmQWERTYUPASDFGHJKZXCVBNM';
		$pattern_numbers = '123456789';
		$password = '';
		$numbers_count = rand(1, $len / 2);

		for($i = 0; $i < $len; $i++){
			$password .= $pattern_letters{rand(0, strlen($pattern_letters) - 1)};
		}
		for($i = 0; $i < $numbers_count; $i++)
		{
			do {
				$number_pos = rand(0, $len -1);
			} while(preg_match('/[0-9]/',$password{$number_pos}) === 1);
			$password{$number_pos} = $pattern_numbers{rand(0, strlen($pattern_numbers) - 1)};
		}
		
		return $password;
	}

	public function deleteByPk($pk, $condition='', $params=array()) {
		$data = array(
			'id_deleted' => 1
		);
		Yii::trace(get_class($this).'.deleteByPk()','system.db.ar.CActiveRecord');
		$builder = $this->getCommandBuilder();
		$criteria = $builder->createPkCriteria($this->getTableSchema(), $pk, $condition, $params);
		$command = $builder->createUpdateCommand($this->getTableSchema(), $data, $criteria);
		return $command->execute();
	}
}